package com.fortune.controller;

import java.util.List;

import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MStoreDeficitExcution;
import com.fortune.dto.in.mStoreDeficit.MStoreDeficitEditDto;
import com.fortune.dto.in.mStoreDeficit.MStoreDeficitListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MStoreDeficitEnum;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.service.MStoreDeficitService;
import io.swagger.annotations.ApiImplicitParams;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.fortune.base.BaseAPIExcution;
import com.fortune.dto.in.mStoreDeficit.MStoreDeficitAddDto;
import com.fortune.vo.mStoreDeficit.MStoreDeficitInfo;
import com.fortune.vo.mStoreDeficit.MStoreDeficitInfoDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value = "mStoreDeficit", description = "厂商仓库报亏", tags = {"厂商仓库报亏"})
@RestController
@RequestMapping("mStoreDeficit")
public class MStoreDeficitController {
	@Autowired
	private MStoreDeficitService mStoreDeficitService;
	Logger logger= LoggerFactory.getLogger(this.getClass());
	
	@ApiOperation(value = "报亏单列表", nickname = "mStoreDeficitList", notes = "", tags={ "@卞兴旺" })
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "第几页", paramType = "query", dataType = "int"),
			@ApiImplicitParam(name = "length", value = "每页容量", paramType = "query", dataType = "int"),
			@ApiImplicitParam(name = "search", value = "搜索关键词", paramType = "query", dataType = "string", required = false),
			@ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
			@ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData> list(HttpServletRequest request, @ApiIgnore @Validated MStoreDeficitListDto mStoreDeficitListDto) {
		mStoreDeficitListDto.setPage((mStoreDeficitListDto.getPage()-1)*10);
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId=mUserInfo.getmId();
		List<MStoreDeficitInfo> list = mStoreDeficitService.findDeficitList(companyId,mStoreDeficitListDto);
		int count = mStoreDeficitService.findDeficitListCount(companyId,mStoreDeficitListDto);
		PagingData pagingData = new PagingData();
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new MStoreDeficitExcution<PagingData>(pagingData, MStoreDeficitEnum.SUCCESS);
	}
	
	@ApiOperation(value = "报亏单详情", nickname = "mStoreDeficitDetail", notes = "", tags={ "@卞兴旺" })
	@ApiImplicitParam(name="id", value="报亏单id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<MStoreDeficitInfoDetail> detail(HttpServletRequest request,@Validated @NotBlank(message="报亏单id不能为空")String id) {
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId=mUserInfo.getmId();
		MStoreDeficitInfoDetail detail=mStoreDeficitService.findDetail(companyId,id);
		return new MStoreDeficitExcution<MStoreDeficitInfoDetail>(detail, MStoreDeficitEnum.SUCCESS);
	}
	
	@ApiOperation(value = "添加报亏单", nickname = "mStoreDeficitAdd", notes = "", tags={ "@卞兴旺" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request, @Validated @RequestBody MStoreDeficitAddDto addDto) {
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId=mUserInfo.getmId();
		String uid=mUserInfo.getId();
		try{
			int addDeficitBase=mStoreDeficitService.addDeficitBase(companyId,uid,addDto);
			if(addDeficitBase>0){
				return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.SUCCESS);
			}else {
				return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.FAILURE);
			}
		}catch (InsertInnerErrorException e){
			logger.error(e.getMessage(), e);
			return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.FAILURE);
		}

	}


	@ApiOperation(value = "确认报亏", nickname = "mStoreDeficitConfirm", notes = "", tags={ "@卞兴旺" })
	@ApiImplicitParam(name="uuid", value="报亏单id",paramType="query",dataType="string",required=true)
	@PostMapping(value = "editConfirm",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> editConfirm(HttpServletRequest request, @Validated @NotBlank(message="报亏单id不能为空") String uuid) {
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId=mUserInfo.getmId();
		String operatorId=mUserInfo.getId();
			int editMStoreDeficitConfirm=mStoreDeficitService.editMStoreDeficitConfirm(companyId,uuid,operatorId);
			if(editMStoreDeficitConfirm>0){
				return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.SUCCESS);
			}else {
				return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.FAILURE);
			}
	}


	@ApiOperation(value = "删除", nickname = "mStoreDeficitDelete", notes = "", tags={ "@卞兴旺" })
	@ApiImplicitParam(name="uuid", value="报亏单id",paramType="query",dataType="string",required=true)
	@DeleteMapping(value = "delete",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request, @Validated @NotBlank(message="报亏单id不能为空") String uuid) {
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId=mUserInfo.getmId();
		int deleteMStoreDeficit=mStoreDeficitService.deleteMStoreDeficit(companyId,uuid);
		if(deleteMStoreDeficit>0){
			return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.SUCCESS);
		}else {
			return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.FAILURE);
		}
	}


	@ApiOperation(value = "编辑报亏单", nickname = "mStoreDeficitEdit", notes = "", tags={ "@卞兴旺" })
	@PostMapping(value = "edit",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request, @Validated @RequestBody MStoreDeficitEditDto editDto) {
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId=mUserInfo.getmId();
		try{
			int editDeficitBase=mStoreDeficitService.editDeficitBase(companyId,editDto);
			if(editDeficitBase>0){
				return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.SUCCESS);
			}else {
				return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.FAILURE);
			}
		}catch (InsertInnerErrorException e){
			logger.error(e.getMessage(), e);
			return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.FAILURE);
		}

	}


	
}
